/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Traffic;

import java.util.ArrayList;

//Zones stores the zones that each part of the model occupies
public class Zones
{
    //Zones that are obsticles such as other vehicles or red lights
    private ArrayList<Zone> zones;
    //Zones that represent a vehicle's location such as a road or the intersection
    private ArrayList<Zone> areas;

    //Constructor: Initialises the two lists
    Zones()
    {
        zones = new ArrayList();
        areas = new ArrayList();
    }

    //Adds a new zone.
    public void addZone(Zone z)
    {
        //If the zone is an intersection or road
        if (z.getState() == ZoneType.INTERSECTION || z.getState() == ZoneType.ROAD || z.getState() == ZoneType.TURN_LANE)
        {
            //It gets added to the area zones
            areas.add(z);
        }
        //Otherwise it's a blockage
        else
        {
            //System.out.println("Blockage Added");
            zones.add(z);
        }
    }

    //Gets the area that the vehicle represented by zone z is in
    public ZoneType getArea(PositionArea z)
    {
        //The return value
        ZoneType zt = ZoneType.UNDEFINED;

        //For each area
        for (Zone area: areas)
        {
            //If the zone overlaps
            if (area.overlaps(z))
            {
                //Since a vehicle could be in overlapping a road and an intersection
                //at the same time, the intersection takes priority.
                if (area.getState() == ZoneType.INTERSECTION)
                {
                    //Assign it as the return value
                    zt = ZoneType.INTERSECTION;
                    //Break the for loop
                    break;
                }
                //If it is not in an intersection then it is on a road
                else
                {
                    zt = area.getState();
                }
            }
        }

        return zt;

    }

    //Tests if a target area is blocked.
    public boolean isBlocked (PositionArea newPos)
    {
        //The value to return
        boolean returnValue = false;

        //For each zone
        for (Zone curZone: zones)
        {
            //If the zone overlaps the new position
            if (newPos.overlaps(curZone.getArea()))
            {
                //If the zone is a blockage
                if (curZone.getState() == ZoneType.BLOCKAGE)
                {
                    //We will need to return true
                    returnValue = true;

                    //break for for loop
                    break;
                }
            }
        }
        //System.out.println("Debug: " + returnValue);
        return returnValue;
    }

    //Gets each of the area zones for the visualisation to use to draw the intersection
    public ArrayList<Zone> getAreas()
    {
        return areas;
    }

    //Removes the zone that is using the passed PositionArea
    void removeZone(PositionArea position)
    {
        Zone zoneToRemove = null;
        //Find the zone we want to remove
        for (Zone z:zones)
        {
            if (z.getArea() == position)
            {
                zoneToRemove = z;
                break;
            }
        }
        //Remove it
        if (zoneToRemove != null)
            zones.remove(zoneToRemove);
    }
}
